from flask import Flask, request
from flask_cors import CORS
import json

from service.CurrmService import CurrmService
from service.StudentCurrmService import StudentCurrmService
from service.StudentService import StudentService
from util.Result import Result

app = Flask(__name__)
CORS(app, supports_credentials=True, resources=r'/*')

studentService = StudentService()
currmService = CurrmService()
studentCurrmServce = StudentCurrmService()


@app.route('/student', methods=['POST'])
def create_student():
    return studentService.create_student(inp=request.json)


@app.route('/student/<int:student_id>', methods=['GET'])
def student(student_id):
    return Result.succ(studentService.getById(student_id).__dict__)


@app.route('/students', methods=['GET', 'OPTIONS'])
def students():
    return Result.succ(studentService.list())


@app.route('/student/<int:student_id>', methods=['DELETE'])
def delete_student(student_id):
    return Result.succ(studentService.delete(studentService.getById(student_id)))


@app.route('/student/<int:student_id>', methods=['PUT'])
def update_student(student_id):
    id = studentService.getById(student_id).id
    return Result.succ(studentService.update(inp=request.json, studentId=id))


@app.route('/student/<int:student_id>/currms', methods=['GET'])
def student_currms(student_id):
    return Result.succ(studentService.selected_currms(student_id))


@app.route('/currm', methods=['POST'])
def create_currm():
    return currmService.create_currm(inp=request.json)


@app.route('/currm/<int:currm_id>', methods=['GET'])
def currm(currm_id):
    return Result.succ(currmService.getById(currm_id).__dict__)


@app.route('/student/<int:student_id>/selectableCurrms', methods=['GET'])
def selectableCurrms(student_id):
    return Result.succ(studentService.selectable_currms(student_id))


@app.route('/currms', methods=['GET', 'OPTIONS'])
def currms():
    return Result.succ(currmService.list())


@app.route('/currm/<int:currm_id>', methods=['DELETE'])
def delete_currm(currm_id):
    return Result.succ(currmService.delete(currmService.getById(currm_id)))


@app.route('/currm/<int:currm_id>', methods=['PUT'])
def update_currm(currm_id):
    id = currmService.getById(currm_id).id
    return Result.succ(currmService.update(inp=request.json, currmId=id))


@app.route('/student/<int:student_id>/currm/<int:currm_id>', methods=['PUT'])
def update_score(student_id, currm_id):
    return Result.succ(studentCurrmServce.update(request.json, student_id, currm_id))


@app.route('/student/<int:student_id>/currm/<int:currm_id>', methods=['DELETE'])
def delete_score(student_id, currm_id):
    return Result.succ(studentCurrmServce.delete(student_id, currm_id))


@app.route('/student/<int:student_id>/currm/<int:currm_id>', methods=['POST'])
def select_currm(student_id, currm_id):
    return Result.succ(studentCurrmServce.create(request.json, student_id, currm_id))


if __name__ == '__main__':
    app.run()
